﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using Xioa_UI.Domain;

namespace Xioa_UI.Controls;

[TemplatePart(Name = "XioaButton", Type = typeof(Button))]
[TemplatePart(Name = "Pop", Type = typeof(Popup))]
public class XioaAutoPopup : XioaDropDown
{
    private Button? _toggleButton;
    private Popup? _popup;

    public override void OnApplyTemplate()
    {
        if (_toggleButton is not null)
        {
            _toggleButton.Click -= ToggleButtonOnClick;
            _toggleButton.MouseEnter -= ToggleButtonOnMouseEnter;
            //_toggleButton.MouseLeave -= PopupOnMouseLeave;
        }

        if (_popup is not null)
        {
            _popup.MouseLeave -= PopupOnMouseLeave;
        }


        _popup = GetTemplateChild("Pop") as Popup;
        _toggleButton = GetTemplateChild("XioaButton") as Button;

        _popup.MouseLeave += PopupOnMouseLeave;
        _popup.MouseEnter += PopupOnMouseEnter;
        if (_toggleButton is not null)
        {
            _toggleButton.MouseLeave += toggleButtonMouseLeave;
            _toggleButton.Click += ToggleButtonOnClick;
            _toggleButton.MouseEnter += ToggleButtonOnMouseEnter;
        }

        base.OnApplyTemplate();
    }

    private bool IsPopupOnMouse { get; set; }

    private void PopupOnMouseEnter(object sender, MouseEventArgs e)
    {
        IsPopupOnMouse = true;
    }

    private void PopupOnMouseLeave(object sender, MouseEventArgs e)
    {
        IsPopupOnMouse = false;
        if ((bool)this.IsChecked!)
            this.IsChecked = false;
    }
    
    private void toggleButtonMouseLeave(object sender, MouseEventArgs e)
    {
        this.Dispatcher.Throttle(() =>
        {
            if ((bool)this.IsChecked!)
            {
                this.IsChecked = false;
                IsPopupOnMouse = false;
            }
        },5000);
        
       
           
    }

    private void ToggleButtonOnClick(object sender, RoutedEventArgs e)
    {
        if (_toggleButton is not null)
            this.IsChecked = !this.IsChecked;
    }

    private void ToggleButtonOnMouseEnter(object sender, MouseEventArgs e)
    {
        if (_toggleButton is not null)
            this.IsChecked = true;
    }

    private void ToggleButtonOnMouseLeave(object sender, MouseEventArgs e)
    {
        if (_toggleButton is not null)
            this.IsChecked = true;
    }

    public static readonly DependencyProperty IsCheckedProperty =
        DependencyProperty.Register("IsChecked", typeof(bool), typeof(XioaAutoPopup),
            new PropertyMetadata(false, (s, e) => { ((XioaAutoPopup)s).IsChecked = (bool)e.NewValue; }));

    public bool? IsChecked
    {
        get => (bool)GetValue(IsCheckedProperty);
        set => SetValue(IsCheckedProperty, value);
    }

    // public Brush TopBackground
    // {
    //     get { return (Brush)GetValue(TopBackgroundProperty); }
    //     set { SetValue(TopBackgroundProperty, value); }
    // }
    //
    // public static readonly DependencyProperty TopBackgroundProperty =
    //     DependencyProperty.Register(nameof(TopBackground), typeof(Brush), typeof(XioaAutoPopup));
    //
    //
    // public Brush CenterBackground
    // {
    //     get { return (Brush)GetValue(CenterBackgroundProperty); }
    //     set { SetValue(CenterBackgroundProperty, value); }
    // }
    //
    // public static readonly DependencyProperty CenterBackgroundProperty =
    //     DependencyProperty.Register(nameof(CenterBackground), typeof(Brush), typeof(XioaAutoPopup));
    //
    //
    // public Brush BottomBackground
    // {
    //     get { return (Brush)GetValue(BottomBackgroundProperty); }
    //     set { SetValue(BottomBackgroundProperty, value); }
    // }
    //
    // public static readonly DependencyProperty BottomBackgroundProperty =
    //     DependencyProperty.Register(nameof(BottomBackground), typeof(Brush), typeof(XioaAutoPopup));
}